package org.minuframework.file.delete;

import java.io.File;

import org.minuframework.lang.StringUtils;
import org.minuframework.exception.ExceptionFailed;
import org.minuframework.file.upload.AbstractFileBuilder;
import org.minuframework.file.upload.FileParamerConfigurer;
import org.minuframework.file.upload.FileTypesConfigurer;
import org.minuframework.file.upload.FileUploadType;

@Deprecated
public class DefaultFileDelete extends AbstractFileBuilder implements FileDeleteFactory{
    
    public DefaultFileDelete(FileParamerConfigurer fileParamerConfigurer, FileTypesConfigurer fileTypesConfigurer, String charset){
        super(fileParamerConfigurer, fileTypesConfigurer, charset);
    }
    
    // ------------------------------------------------------------------------
    
    /**
     * 批量删除文件
     * @param fileNames 文件路径、名称 String[]
     * @param type      文件类型 FileUploadType
     * @return boolean
     * @throws ExceptionFailed
     */
    public boolean deleteFile(String fileName, FileUploadType type) throws ExceptionFailed{
        boolean flag = false;
        
        if (StringUtils.isNotEmpty(fileName)) {
            File path = getFile(fileName, type);
            if (!isNotFile(path)) {
                File file = new File(path, fileName);
                if (file.exists()) {
                    flag = file.delete();
                } else {
                    throw new ExceptionFailed("Cannot find file from " + path);
                }
            } else {
                throw new ExceptionFailed("Cannot find folder from " + path);
            }
        }
        
        return flag;
    }
    
    /**
     * 批量删除文件
     * @param fileNames 文件路径、名称 String[]
     * @param type      文件类型 FileUploadType
     * @return boolean
     * @throws ExceptionFailed
     */
    public boolean deleteFile(String[] fileNames, FileUploadType type) throws ExceptionFailed{
        boolean flag = false;
        if (fileNames.length <= 0) { return false; }
        for (String fileName : fileNames) {
            flag = deleteFile(fileName, type);
        }
        return flag;
    }
    
}
